const App = {
    mixins: [Query],//配置文件，路径/utils/mixins.js
    data() {
        return {
            activeName: '',
            activeName1: '1',
            getYMDDate,
            page: {
                value1: '',
                PageNumber: 1, //页码
                RowAmount: 50,
                Condition: window.btoa(unescape(encodeURIComponent("%%")))
            },
            Room_Type_SerialNumber: '',
            House_Room_SerialNumber: '',
            room_type_bookings: [],
            room_type_check_ins: {},
            dianhuayy: {},
            showDialog1: false,
            QR_code: false,
            form: {},
            tab: [{
                Room_Type_SerialNumber: '',
                Room_Type_Name: '全部'
            }],
            row: {},
            count: [],
        }
    },
    mounted() {
        console.log(encodeURIComponent('0007-1-0106')); 
    },
    methods: {
        handleshowDialog() {
            this.showDialog = false;
            this.room_type_check_ins = {};
            
        },
        getTotal({id}){
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 't_count_room_type',
                    Condition: window.btoa(unescape(encodeURIComponent("%" + id + "%,%" + id + "%,%" + id + "%"))),
                    ExtensionCondition: '',
                },
                success: (data) => {
                    this.count = data.map(i => i.Total);
                    console.log(this.count);
                },
            })
        },
        // submitSave() {
        //     this.websocket = new WebSocketClient();
        //     this.websocket.init({
        //         url: `${ws}/generalSave/${getCookie('token')}`,
        //         onopen: () => {
        //             console.log('websocket已开启');
        //         },
        //         onmessage: (data) => {
        //             try {
        //                 const res = JSON.parse(data);
        //                 if (res.REDIRECT && res.REDIRECT == 'REDIRECT') {//重定向
        //                     removeCookie("token");
        //                     removeCookie("user");
        //                     sessionStorage.setItem("REDIRECT", "登录已过期，请重新登录！");
        //                     window.parent.document.location.href = absolutepath() + res.CONTENTPATH;
        //                 } else {
        //                     if (res.Action == 'Delete') {//删除
        //                         if (res.Status == 'success') {
        //                             this.postgetList(this.page);
        //                         } else {
        //                             this.$message({
        //                                 type: 'error',
        //                                 message: '删除失败'
        //                             });
        //                         }
        //                     } else if (res.Action == 'Allocation') {//删除
        //                         if (res.Status == 'success') {
        //                             this.showDialog = false;
        //                             this.room_type_check_ins = {};
        //                             this.postgetList(this.page);
        //                         } else {
        //                             this.$message({
        //                                 type: 'error',
        //                                 message: '分配失败'
        //                             });
        //                         }
        //                         this.loading = false;
        //                     }
        //                 }
        //             } catch (error) { }
        //         },
        //     })
        //     window.addEventListener("beforeunload", () => {
        //         this.websocket.close();
        //     });
        // },
        getList() {
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 's_base_sql_table_config',
                    Condition: window.btoa(unescape(encodeURIComponent("%" + this.relativePath + "%"))),
                    ExtensionCondition: '',
                },
                success: (data) => {
                    that.columns = data;
                    that.checkedFields = data;
                    that.fields = data;
                    // that.loading = true;
                    // that.postgetList(that.page);
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest)
                },
            });
        },
        check() {
            this.page.Condition = window.btoa(unescape(encodeURIComponent("%%,%" + this.page.value1 + "%")));
            this.postgetList(this.page)
        },
        changeCheck(val) {
            if (val) {
                let obj = this.room_type_bookings.filter(i => i.Room_Type_Bookings_SerialNumber = val)[0]
                if (obj) {
                    this.room_type_check_ins = {
                        Merchant_SerialNumber: obj.Merchant_SerialNumber,
                        House_Room_SerialNumber: this.House_Room_SerialNumber,
                        Room_Type_SerialNumber: obj.Room_Type_SerialNumber,
                        Room_Type_Bookings_SerialNumber: val,
                        Guest_Name: obj.Guest_Name,
                        ID_Card_Number: obj.ID_Card_Number,
                        Contact_Phone: obj.Contact_Phone,
                        Check_In_Date: obj.Check_In_Date,
                        Check_Out_Date: obj.Check_Out_Date,
                        Row_Status: '0'
                    }
                }
            }
        },
        handledy(row) {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 'f_room_type_bookings',
                    Condition: window.btoa(unescape(encodeURIComponent(row.Room_Type_SerialNumber))),
                    ExtensionCondition: '',
                },
                success: (data) => {
                    console.log(row);
                    this.room_type_bookings = data;
                    this.row = row;
                    this.House_Room_SerialNumber = row.House_Room_SerialNumber;
                    this.showDialog = true;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            })
        },
        allocation() {
            this.loading = true;
            if (this.activeName1 == '1') {

                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    type: "POST",
                    data: {
                        Data: "room_type_bookings",
                        Condition: "Room_Type_Bookings_SerialNumber='" + this.room_type_check_ins.Room_Type_Bookings_SerialNumber + "'",
                    },
                    success: (room_type_bookings) => {
                        room_type_bookings[0].House_Room_SerialNumber = this.House_Room_SerialNumber;
                        room_type_bookings[0].Row_Status = '5';
                        let lJSONObject_DataSet = {
                            Add: {
                                room_type_check_ins: [this.room_type_check_ins]
                            },
                            Modify: {
                                room_type_bookings
                            }
                        }
                        submitSave(lJSONObject_DataSet, (res) => {
                            if (res.Status == 'success') {
                                this.showDialog = false;
                                this.room_type_check_ins = {};
                                this.postgetList(this.page);
                            } else {
                                this.$message({
                                    type: 'error',
                                    message: '分配失败'
                                });
                            }
                            this.loading = false;
                        })
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                })
            } else if (this.activeName1 == '2') {
                let lJSONObject_DataSet = {
                    Add: {
                        room_type_check_ins: [{
                            ...this.room_type_check_ins,
                            Merchant_SerialNumber: this.row.Merchant_SerialNumber,
                            Room_Type_SerialNumber: this.row.Room_Type_SerialNumber,
                            House_Room_SerialNumber: this.House_Room_SerialNumber,
                            Row_Status: '2'
                        }],
                        room_type_bookings: [{
                            ...this.room_type_check_ins,
                            Merchant_SerialNumber: this.row.Merchant_SerialNumber,
                            Room_Type_SerialNumber: this.row.Room_Type_SerialNumber,
                            House_Room_SerialNumber: this.House_Room_SerialNumber,
                            Row_Status: '4'
                        }]
                    },
                }
                submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status == 'success') {
                        this.showDialog = false;
                        this.room_type_check_ins = {};
                        this.postgetList(this.page);
                    } else {
                        this.$message({
                            type: 'error',
                            message: '分配失败'
                        });
                    }
                    this.loading = false;
                })
                // this.websocket.send(JSON.stringify({ Data: lJSONObject_DataSet, Action: 'Allocation', AccessToken: getCookie('token') }));
            } else if (this.activeName1 == '3') {
                let lJSONObject_DataSet = {
                    Add: {
                        room_type_check_ins: [{
                            ...this.room_type_check_ins,
                            Merchant_SerialNumber: this.row.Merchant_SerialNumber,
                            Room_Type_SerialNumber: this.row.Room_Type_SerialNumber,
                            House_Room_SerialNumber: this.House_Room_SerialNumber,
                            Row_Status: '0'
                        }],
                        room_type_bookings: [{
                            ...this.room_type_check_ins,
                            Merchant_SerialNumber: this.row.Merchant_SerialNumber,
                            Room_Type_SerialNumber: this.row.Room_Type_SerialNumber,
                            House_Room_SerialNumber: this.House_Room_SerialNumber,
                            Row_Status: '5'
                        }]
                    },
                }
                submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status == 'success') {
                        this.showDialog = false;
                        this.room_type_check_ins = {};
                        this.postgetList(this.page);
                    } else {
                        this.$message({
                            type: 'error',
                            message: '分配失败'
                        });
                    }
                    this.loading = false;
                })
                // this.websocket.send(JSON.stringify({ Data: lJSONObject_DataSet, Action: 'Allocation', AccessToken: getCookie('token') }));
            }

        },
        handletf(row) {
            this.$confirm('该操作将退房,是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/queryMultiTableByCondition`,
                    method: "post",
                    data: {
                        Data: JSON.stringify([{
                            tableName: 'room_type_check_ins',
                            Condition: window.btoa(unescape(encodeURIComponent("House_Room_SerialNumber='" + row.House_Room_SerialNumber + "' AND Room_Type_SerialNumber='" + row.Room_Type_SerialNumber + "' AND (Row_Status='0' OR Row_Status='2')"))),
                        }, {
                            tableName: 'room_type_bookings',
                            Condition: window.btoa(unescape(encodeURIComponent("House_Room_SerialNumber='" + row.House_Room_SerialNumber + "' AND Room_Type_SerialNumber='" + row.Room_Type_SerialNumber + "' AND (Row_Status='4' OR Row_Status='5')"))),
                        }]),
                    },
                    success: ({ room_type_check_ins, room_type_bookings }) => {

                        room_type_check_ins[0].Row_Status = '3';
                        let room_cleaning = [];
                        if (row.Row_Status == '2' && room_type_bookings.length > 0) {
                            room_type_bookings[0].Row_Status = '3'
                        } else if (row.Row_Status == '0' && room_type_bookings.length > 0) {
                            room_type_bookings[0].Row_Status = '6'
                            // let now = new Date();
                            // now.setMinutes(now.getMinutes() + 30);
                            // room_cleaning.push({
                            //     Room_Type_SerialNumber: row.Room_Type_SerialNumber,
                            //     Merchant_SerialNumber: row.Merchant_SerialNumber,
                            //     Merchant_Mapping_Blue_Plate_SerialNumber: row.Merchant_Mapping_Blue_Plate_SerialNumber,
                            //     House_Room_SerialNumber: row.House_Room_SerialNumber,
                            //     Cleaning_Datetime:getYMDHMSDate(now),
                            // })
                        }

                        let lJSONObject_DataSet = {
                            Modify: {
                                room_type_check_ins,
                                room_type_bookings
                            }
                        }
                        submitSave(lJSONObject_DataSet, (res) => {
                            if (res.Status == 'success') {
                                this.showDialog = false;
                                this.room_type_check_ins = {};
                                this.postgetList(this.page);
                            } else {
                                this.$message({
                                    type: 'error',
                                    message: '分配失败'
                                });
                            }
                            this.loading = false;
                        })
                        // this.websocket.send(JSON.stringify({ Data: lJSONObject_DataSet, Action: 'Allocation', AccessToken: getCookie('token') }));
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                })

            }).catch(() => { });
        },
        handleqs(row) {
            this.$confirm('该操作将确认该房间清扫完成,是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/queryMultiTableByCondition`,
                    method: "post",
                    data: {
                        Data: JSON.stringify([{
                            tableName: 'room_type_check_ins',
                            Condition: window.btoa(unescape(encodeURIComponent("House_Room_SerialNumber='" + row.House_Room_SerialNumber + "' AND Room_Type_SerialNumber='" + row.Room_Type_SerialNumber + "' AND Row_Status='3'"))),
                        }]),
                    },
                    success: ({ room_type_check_ins }) => {
                        room_type_check_ins[0].Row_Status = '1';
                        let room_cleaning = [];

                        let now = new Date();
                        now.setMinutes(now.getMinutes() + 30);
                        room_cleaning.push({
                            Room_Type_SerialNumber: row.Room_Type_SerialNumber,
                            Merchant_SerialNumber: row.Merchant_SerialNumber,
                            Merchant_Mapping_Blue_Plate_SerialNumber: row.Merchant_Mapping_Blue_Plate_SerialNumber,
                            House_Room_SerialNumber: row.House_Room_SerialNumber,
                            Cleaning_Datetime: getYMDHMSDate(now),
                        })

                        let lJSONObject_DataSet = {
                            Modify: {
                                room_type_check_ins,
                            },
                            Add: {
                                room_cleaning
                            }
                        }
                        submitSave(lJSONObject_DataSet, (res) => {
                            if (res.Status == 'success') {
                                this.showDialog = false;
                                this.room_type_check_ins = {};
                                this.postgetList(this.page);
                            } else {
                                this.$message({
                                    type: 'error',
                                    message: '清扫失败'
                                });
                            }
                            this.loading = false;
                        })
                        // this.websocket.send(JSON.stringify({ Data: lJSONObject_DataSet, Action: 'Allocation', AccessToken: getCookie('token') }));
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                })

            }).catch(() => { });
        },
        Treedata() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: "merchant",
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                        method: "post",
                        data: {
                            SQLSerialNumber: 'f_merchant_business_mapping_blue_plate',
                            Condition: '',
                            ExtensionCondition: '',
                        },
                        success: (data1) => {
                            let treeData = [];
                            data.forEach(item => {
                                let arr = data1.filter(i => i.Merchant_SerialNumber == item.Merchant_SerialNumber)
                                if (arr.length > 0) {
                                    let children = [];
                                    arr.forEach(obj => {
                                        children.push({
                                            id: obj.Merchant_Mapping_Blue_Plate_SerialNumber,
                                            Merchant_Category_SerialNumber: obj.Merchant_Category_SerialNumber,
                                            Blue_Plate_SerialNumber: obj.Blue_Plate_SerialNumber,
                                            name: obj.Merchant_Alias,
                                        })
                                    });
                                    treeData.push({
                                        id: item.Merchant_SerialNumber,
                                        name: item.Merchant_Name,
                                        children,
                                    })
                                }
                            });
                            this.treeData = treeData;
                            this.handleNodeClick(treeData[0].children[0], {level:2})
                            this.getData(treeData[0].children[0])
                            this.getTotal(treeData[0].children[0]);
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json",
                    });
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        getData({ id }) {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 'f_room_type',
                    Condition: window.btoa(unescape(encodeURIComponent(id))),
                    ExtensionCondition: '',
                },
                success: (data) => {
                    let arr = [{
                        Room_Type_SerialNumber: '',
                        Room_Type_Name: '全部'
                    }, ...data]
                    this.tab = arr;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        handlexq(row) {
            this.form = row;
            this.QR_code = true;
            this.$nextTick(() => {
                const qrElementId = `qr-${row.House_Room_SerialNumber}`;
                const qrElement = document.getElementById(qrElementId);
                // 清空原有二维码（如果存在）
                if (qrElement) qrElement.innerHTML = "";

                // 优化后的二维码生成参数
                new QRious({
                    element: qrElement,
                    value: `https://service.essp.top/mh/h5/room/index.html?id1=${row.Merchant_SerialNumber}&id2=${row.Room_Type_SerialNumber}&id3=${row.House_Room_SerialNumber}`,
                    size: 180, // 增大尺寸到256px
                    level: "H", // 最高容错率（可恢复30%损坏）
                    background: "#ffffff", // 明确背景色
                    foreground: "#000000", // 明确前景色
                    quietZone: 4, // 增加空白边距
                });

                // 生成成功后添加动画效果
                if (qrElement) {
                    qrElement.style.transition = "all 0.5s ease";
                    qrElement.style.opacity = "0";
                    setTimeout(() => {
                        qrElement.style.opacity = "1";
                    }, 100);
                }
            });
        },
        //树的点击筛选事件
        handleNodeClick(data, node) {
            console.log(data);
            if (node.level == 1) {
                // this.page.Condition = window.btoa(unescape(encodeURIComponent("%%,%" + data.children[0].Merchant_Category_SerialNumber + "%,%" + data.Blue_Plate_SerialNumber + "%")))
                // this.getData(data);
            } else if (node.level == 2) {
                console.log(data.Blue_Plate_SerialNumber);
                this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + data.id + "%")))
                this.getData(data);
                this.getTotal(data);
            }
            this.postgetList(this.page);
        },
    },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-select', ylSelect)
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
